package com.xm.event.order;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 订单服务
 */
@Slf4j
public class OrderService {
    private final static List<OrderListener> listeners = new CopyOnWriteArrayList<OrderListener>();

    static {
        listeners.add(new LogListener());
        listeners.add(new InventoryListener());
    }

    //注册监听器
    public void addOrderListener(OrderListener listener) {
        listeners.add(listener);
    }

    //触发事件
    public void createOrder(String orderId) {
        log.info("订单服务：订单[{}]正在生成中。。", orderId);
        OrderEvent orderEvent = new OrderEvent(this, orderId);
        for (OrderListener listener : listeners) {
            listener.onOrderCreated(orderEvent);
        }
        log.info("--------------------------");
    }
}
